from string import upper

class StateMachine :
    def __init__ (self):
        self.handlers = {}
        self.startState = None
        self.endStates = []
        
    def add_state (self, name, handler, end_state=0):
        name = upper(name)
        self.handlers[name] = handler
        if end_state:
            self.endStates.append(name)
        
    def set_start (self, name):
        self.startState = upper(name)
        
    def run (self, cargo):
        try :
            handler = self.handlers[self.startState]
        except :
            raise 'InitializationError',  'must call .set_start() before .run()'
        if not self.endStates:
            raise 'InitializationError',  'at least one state must be an end_state'
        
        while 1:
            (newState, cargo) = handler(cargo)
            if upper(newState) in self.endStates:
                break
            else :
                handler = self.handlers[upper(newState)]



if __name__ == '__main__':
    class StateHandler_WD:
        '''handling word and digit'''
        def __init__(self, txt):
            print "SRC:"+txt
            self.__txt = txt
            self.__max_len = len(txt)
            self.__cur_idx = 0
            self.words = []
            self.digits = []
            
        def next_char(self):
            self.__cur_idx += 1
            if self.__cur_idx < self.__max_len :
                return self.__txt[self.__cur_idx]
            else :
                self.__cur_idx = self.__max_len
                return '\0'
        
        def hdl_begin(self, val):
            """handler:BEGIN"""
            new_state='_'
            val=self.next_char()
            if val=='\0' :
                new_state='END'
            elif val.isdigit():
                new_state='_D'
            elif val.isalpha():
                new_state='_W'
                
            return (new_state, val)
        
        def hdl_word(self, val):
            """handler:_WORD"""
            new_state='_W'
            idx_start=self.__cur_idx
            while 1 :
                val=self.next_char()
                if val=='\0' :
                    new_state='END';break
                elif val.isdigit():
                    new_state='_D';break
                elif val.isalpha():
                    continue
                else:
                    new_state='_';break
            self.words.append( self.__txt[idx_start:self.__cur_idx] )
            return (new_state, val)
        
        def hdl_digit(self, val):
            """handler:_DIGIT"""
            new_state='_D'
            idx_start=self.__cur_idx
            while 1 :
                val=self.next_char()
                if val=='\0' :
                    new_state='END';break
                elif val.isalpha():
                    new_state='_W';break
                elif val.isdigit():
                    continue
                else:
                    new_state='_';break
            self.digits.append( self.__txt[idx_start:self.__cur_idx] )
            return (new_state, val)
        
        def hdl_mess(self, val):
            """handler:_MESS"""
            new_state='_'
            while 1 :
                val=self.next_char()
                if val=='\0' :
                    new_state='END';break
                elif val.isdigit():
                    new_state='_D';break
                elif val.isalpha():
                    new_state='_W';break
                else:
                    new_state='_';break
            return (new_state, val)
        
        def hdl_end(self, val):
            """handler:END"""
            self.__max_len = len(txt)
            self.__cur_idx = 0
            return ('', val)
        
    wd = StateHandler_WD('  absdfEFdsf,fdssaf fwef 123sfe asdfds23 232,2343456')
    sm = StateMachine()
    sm.add_state('BEGIN', wd.hdl_begin)
    sm.add_state('_W', wd.hdl_word)
    sm.add_state('_D', wd.hdl_digit)
    sm.add_state('_', wd.hdl_mess)
    sm.add_state('END', wd.hdl_end, end_state=1)
    sm.set_start('BEGIN')
    sm.run('')
    
    print 'WORDS FROM WD:'
    for word in wd.words:
        print word
    
    print 'DIGITS FROM WD:'
    for digit in wd.digits:
        print digit
    

